<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">姓名：</label>
                                    <div class="layui-input-inline">
                                        <input name="name" class="layui-input" type="text"
                                               placeholder="请输入姓名"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">电话：</label>
                                    <div class="layui-input-inline">
                                        <input name="phone" class="layui-input" type="text"
                                               placeholder="请输入电话"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" layui-form-keyDownSearch="true" lay-submit>查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="agentContactsBtnAdd" class="layui-btn icon-btn color-reseda">添加
                            </button>
                            <button id="agentContactsBtnExport" class="layui-btn icon-btn color-reseda">导出
                            </button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="agentContactsTable" lay-filter="agentContactsTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="agentContactsTableBar">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="agentContactsForm">
    <form lay-filter="agentContactsForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-block">
                    <input name="name" placeholder="请输入姓名" type="text" class="layui-input" lay-verify="required"
                           required/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label">电话</label>
                <div class="layui-input-block">
                    <input name="phone" placeholder="请输入电话" type="text" class="layui-input" lay-verify="required"
                           required/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-block">
                    <select name="sex">
                        <option value="man">男</option>
                        <option value="woman">女</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label">邮箱</label>
                <div class="layui-input-block">
                    <input name="email" placeholder="请输入邮箱" type="text" class="layui-input" lay-verify="required"
                           required/>
                </div>
            </div>
        </div>

        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label">照片</label>
                <div id="btnUpload14" class="layui-btn" style="float: left;"><i class="layui-icon">&#xe681;</i>上传照片
                </div>
                <div id="btnUpload14_path" style="margin-left: 260px;line-height: 40px;"></div>
                <input id="photo" name="photo" type="hidden"/>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label">名片</label>
                <div id="btnUpload15" class="layui-btn" style="float: left;"><i class="layui-icon">&#xe681;</i>上传名片
                </div>
                <div id="btnUpload15_path" style="margin-left: 260px;line-height: 40px;"></div>
                <input id="businessCard" name="businessCard" type="hidden"/>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label">微信号</label>
                <div class="layui-input-block">
                    <input name="wx" placeholder="请输入微信号" type="text" class="layui-input" lay-verify="required"
                           required/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-block">
                    <input name="sortNumber" placeholder="请输入排序" type="text" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label">所属代理商</label>
                <div class="layui-input-block">
                    <select lay-filter="selAgtId" id="selAgtId" name="selAgtId" lay-verify="required">
                        <option value="">请选择代理商</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label">所属职务</label>
                <div class="layui-input-block">
                    <select lay-filter="agtPostId" id="agtPostId" name="agtPostId" lay-verify="required">
                        <option value="">请选择职务</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label">所属部门</label>
                <div class="layui-input-block">
                    <input name="agtDept" placeholder="请输入所属部门" type="text" class="layui-input"
                           lay-verify="required"
                           required/>
                </div>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="agentContactsFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin',
        'layedit', 'tableTreeDj', 'newTree', 'laydate', 'upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var layedit = layui.layedit;
        var upload = layui.upload;
        var tableTree = layui.tableTreeDj;
        var newTree = layui.newTree;
        var laydate = layui.laydate;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#agentContactsTable',
            url: config.base + 'enterprise/sell/admin/agentContacts/list',
            toolbar: true,
            defaultToolbar: ["filter"],
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'}
                , {
                    field: 'photo', align: 'center', title: '照片'
                    , templet: '<div><img src="{{ d.photo}}"></div>'
                }
                , {field: 'name', align: 'center', title: '姓名'}
                , {field: 'phone', align: 'center', title: '电话'}
                , {field: 'email', align: 'center', title: '邮箱', width: 180}
                , {field: 'wx', align: 'center', title: '微信号'}
                , {field: 'sexDesc', align: 'center', title: '性别', width: 80}
                , {field: 'selAgtName', align: 'center', title: '所属代理商'}
                , {field: 'agtPostName', align: 'center', title: '所属职务'}
                , {field: 'agtDept', align: 'center', title: '所属部门'}
                , {field: 'createTime', align: 'center', title: '创建时间', width: 170}
                , {align: 'center', toolbar: '#agentContactsTableBar', title: '操作', width: 150}
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if (menuButton.indexOf("搜索") == -1) {
                    $('.layui-form.toolbar').remove();
                }
                if (menuButton.indexOf("修改") == -1) {
                    $("[lay-event='edit']").remove();
                }
                if (menuButton.indexOf("删除") == -1) {
                    $("[lay-event='del']").remove();
                }
            }
        });

        // 工具条点击事件
        table.on('tool(agentContactsTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(agentContactsTable)', function (obj) {
            table.reload('agentContactsTable', {
                initSort: obj
                , where: {
                    sort: obj.field
                    , order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            let field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });

        // 添加按钮点击事件
        $('#agentContactsBtnAdd').click(function () {
            showEditModel();
        });

        /**
         * 加载地址HTML
         * @param data 下拉菜单数据
         * @param tips select 默认提示
         * @param htmltarget html要插入的位置
         * @param name  下拉框需要显示值的属性
         */
        function loadSelectHtml(data, tips, htmltarget, name) {
            let html = ' <option value>' + tips + '</option>';
            if (!data || data == null) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                let isFinalCategory = '';
                if (data[i].isFinalCategory != undefined) {
                    isFinalCategory = `isFinal=` + data[i].isFinalCategory;
                }
                html += `<option value='${data[i].id}' ${isFinalCategory}>${data[i][name]}</option>`;
            }
            $("[name='" + htmltarget + "']").html(html);
            form.render();
        }

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '800px',
                offset: '65px',
                title: data ? '修改销售代理商经销联系人' : '添加销售代理商经销联系人',
                content: $('#agentContactsForm').html(),
                success: function () {

                    //查询企业信息
                    var enterpriseInfo;
                    admin.req('enterprise/system/admin/enterpricceId/getEnterpricceIdVO', {}, function (res) {
                        enterpriseInfo = res.data;
                    }, 'get', false);
                    if ("" == enterpriseInfo || null == enterpriseInfo) {
                        layer.msg("企业信息获取异常", {icon: 1});
                        return;
                    }

                    //上传照片
                    upload.render({
                        elem: '#btnUpload14'
                        , url: config.upload_url + 'uploadFile'
                        , data: {"path": enterpriseInfo.enterpriseCode + "/"}
                        , accept: 'file'
                        , exts: 'jpg|png|RAR|zip|pdf|txt|doc|docx|xls|xlsx|ppt|pptx'
                        , headers: {Authorization: 'Bearer ' + config.getToken()}
                        , done: function (res) {
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                var imagUrl = res.data.url;
                                // if(imagUrl){
                                //     imagUrl=imagUrl.substring(imagUrl.indexOf('/')+1)
                                // }
                                $('#btnUpload14_path').html('<img src="' + imagUrl + '" style="height: 50px;" />');
                                $('#photo').val(imagUrl);
                            } else if (res.code == 502) {
                                return location.replace('admin/login.html');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }
                    });
                    if (data && data.photo) {
                        $('#btnUpload14_path').html('<img src="' + data.photo + '" style="height: 50px;" />');
                    }

                    //上传名片
                    upload.render({
                        elem: '#btnUpload15'
                        , url: config.upload_url + 'uploadFile'
                        , data: {"path": enterpriseInfo.enterpriseCode + "/"}
                        , accept: 'file'
                        , exts: 'jpg|png|RAR|zip|pdf|txt|doc|docx|xls|xlsx|ppt|pptx'
                        , headers: {Authorization: 'Bearer ' + config.getToken()}
                        , done: function (res) {
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                var imagUrl = res.data.url;
                                // if(imagUrl){
                                //     imagUrl=imagUrl.substring(imagUrl.indexOf('/')+1)
                                // }
                                $('#btnUpload15_path').html('<img src="' + imagUrl + '" style="height: 50px;" />');
                                $('#businessCard').val(imagUrl);
                            } else if (res.code == 502) {
                                return location.replace('admin/login.html');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }
                    });
                    if (data && data.businessCard) {
                        $('#btnUpload15_path').html('<img src="' + data.businessCard + '" style="height: 50px;" />');
                    }

                    admin.req('enterprise/sell/admin/agent/list', {
                        limit: 99999,
                        validity: 0,
                        status: '2,3'
                    }, function (res) {
                        loadSelectHtml(res.data, "请选择代理商", "selAgtId", "name");
                        if (data) {
                            $("#selAgtId").val(data.selAgtId);
                            form.render('select');
                        }
                    }, 'get', false);

                    admin.req('enterprise/sell/admin/customerContactPost/list', {limit: 99999}, function (res) {
                        loadSelectHtml(res.data, "请选择职务", "agtPostId", "name");
                        if (data) {
                            $("#agtPostId").val(data.selAgtId);
                            form.render('select');
                        }
                    }, 'get', false);

                    admin.req('enterprise/sell/admin/customerContactDept/list', {limit: 99999}, function (res) {
                        loadSelectHtml(res.data, "请选择部门", "agtDeptId", "name");
                        if (data) {
                            $("#agtDeptId").val(data.selAgtId);
                            form.render('select');
                        }
                    }, 'get', false);


                    if (data) {
                        var option = $("form select[name='sex']").find("option");
                        layui.each(option, function (index, item) {
                            if (data.sex == item.text) {
                                data.sex = item.value;
                            }
                        });
                    }

                    form.val('agentContactsForm', data);
                    // 表单提交事件
                    form.on('submit(agentContactsFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req(data ? 'enterprise/sell/admin/agentContacts/update' : 'enterprise/sell/admin/agentContacts/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('agentContactsTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/sell/admin/agentContacts/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#agentContactsBtnExport').click(function () {
            var checkRows = table.checkStatus('agentContactsTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>